package edu.nepu.bigdata.redis;

import edu.nepu.bigdata.compile.DynamicCompileAndRun;
import edu.nepu.bigdata.constant.Constant;
import edu.nepu.bigdata.utils.CommandLineParser;
import edu.nepu.bigdata.utils.JedisPikaUtil;
import edu.nepu.bigdata.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisSentinelPool;

public class ConnectRedis {

    public static void main(String[] args) throws Exception{
        Logger logger = LoggerFactory.getLogger(ConnectRedis.class);
        CommandLineParser.parseRedisArgs(args);
        String connectType = CommandLineParser.getArg("connectType");
        DynamicCompileAndRun dynamicCompileAndRun = new DynamicCompileAndRun(CommandLineParser.getArg("fullClassName"), CommandLineParser.getArg("methodName"));
        if (connectType.equalsIgnoreCase(Constant.REDIS_SENTINEL)){
            JedisSentinelPool pikaSentinelPool = JedisPikaUtil.getPikaSentinelPool(CommandLineParser.getArg("hostPort"), CommandLineParser.getArg("pikaMaster"), CommandLineParser.getArg("password"));
            Jedis jedis = pikaSentinelPool.getResource();
            dynamicCompileAndRun.run(jedis,null);
            jedis.close();
            pikaSentinelPool.close();
            return;
        }
        if (connectType.equalsIgnoreCase(Constant.REDIS_CLUSTER)){
            JedisCluster jedisCluster = RedisUtil.getJedisCluster(CommandLineParser.getArg("hostPort"), CommandLineParser.getArg("password"));
            dynamicCompileAndRun.run(null,jedisCluster);
            jedisCluster.close();
            return;
        }
        if(connectType.equalsIgnoreCase(Constant.REDIS_SINGLE)){
            String[] hostAndPorts = CommandLineParser.getArg("hostPort").split(":");
            Jedis jedis = new Jedis(hostAndPorts[0], Integer.parseInt(hostAndPorts[1]));
            dynamicCompileAndRun.run(jedis,null);
            jedis.close();
        }
    }
}
